import cc

from google.appengine.ext import db
from google.appengine.runtime import DeadlineExceededError
	
class ResultTuple(db.Model): 
	request_id = db.StringProperty()
	finish_time = db.DateTimeProperty(auto_now_add=True)
	bit_diff = db.IntegerProperty() 
	closest_checksum = db.IntegerProperty() 
	best_match_string = db.StringProperty(multiline=True)
		
def performSearch(document1, document2, requestID, startString, maxLength):
	try:
		diff=121212112313123123123
		result = ResultTuple()
		for i in range(1, maxLength):
			bitDiff, checksum, output = cc.findmatch(startString, document2, document1, i, requestID)
			if bitDiff==0:
				result.request_id = requestID
				result.bit_diff = bitDiff
				result.closest_checksum = checksum
				result.best_match_string = output
				break
			if(bitDiff < diff):
				diff=bitDiff				
				result.request_id = requestID
				result.bit_diff = bitDiff
				result.closest_checksum = checksum
				result.best_match_string = output
	except DeadlineExceededError:
		result.put()
	else:
		result.put()
		

